package 下一个排列;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
//        int[] nums = new int[]{1,6,9,8};
//        int[] nums = new int[]{1,8,6,9};
//        int[] nums = new int[]{1,8,9,6};
//        int[] nums = new int[]{1,9,6,8};
        int[] nums = new int[]{3,2,1};
        solution.nextPermutation(nums);
    }
    public void nextPermutation(int[] nums) {

        for (int num : nums) {
            System.out.print(num);
        }
        System.out.println();

        int i = nums.length -2;
        int j = nums.length -1;
        int index = nums.length - 1;
        outer: for(;i >= 0;i--,j--){
            if(nums[i] < nums[j]){
                for(;index > i;index--){
                    if(nums[index] > nums[i]){
                        int temp = nums[index];
                        nums[index] = nums[i];
                        nums[i] = temp;

                        break outer;
                    }
                }
            }
        }
        index = nums.length - 1;
        for(; index > j;index--,j++ ){
            int temp1 = nums[index];
            nums[index] = nums[j];
            nums[j] = temp1;
        }



        for (int num : nums) {
            System.out.print(num);

        }
    }
}
